package at.fhooe.mcm.logic.logging;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import at.fhooe.mcm.logic.core.Core;
import at.fhooe.mcm.logic.core.Service;

public abstract class LoggingService implements Service
{
	public static final int		DEBUG		= 3;
	public static final int		INFO		= 2;
	public static final int		WARNING		= 1;
	public static final int		ERROR		= 0;
	
	protected Core				m_core;
	protected int				m_level;
	
	@Override
	public void initialize(Core _core)
	{
		m_core = _core;
	}
	
	public abstract String getName();

	public void logDebug(String _source, String _message)
	{
		log(DEBUG, _source, _message);
	}

	public void logInfo(String _source, String _message)
	{
		log(INFO, _source, _message);
	}

	public void logWarning(String _source, String _message)
	{
		log(WARNING, _source, _message);
	}

	public void logError(String _source, String _message)
	{
		log(ERROR, _source, _message);
	}

	public int getLevel()
	{
		return m_level;
	}

	public void setLevel(int _level)
	{
		if (_level < ERROR || _level > DEBUG)
		{
			throw new IllegalArgumentException("invalid logging level: " + _level);
		}
		m_level = _level;
	}
	
	protected String timeToString(Date _date)
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
	
		return dateFormat.format(_date);
	}

	protected String levelToText(int _level)
	{
		switch (_level)
		{
		case DEBUG:
			return "DEBUG";
		case INFO:
			return "INFO";
		case WARNING:
			return "WARNING";
		case ERROR:
			return "ERROR";
		}

		throw new RuntimeException("illegal logging level");
	}
	
	protected abstract void log(int _level, String _source, String _message);
}
